package com.fysports.web.model.system;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.fysports.web.kit.commons.DateTimeKit;
import com.fysports.web.kit.commons.UUIDKit;
import com.fysports.web.model.BaseModel;

import java.util.List;


/**
 * Created by konbluesky
 * Date : 14-8-4 下午11:23
 * Project : JdmData_WebServer
 */
public class SystemUser extends BaseModel<SystemUser> {
    private static Log log = Log.getLog(SystemUser.class);
    public final static SystemUser dao = new SystemUser();
    public final static String COL_PWD = "pwd";


    public SystemUser() {
        if (this.getStr(COL_UUID) == null || this.getStr(COL_UUID).length() == 0) {
            this.set(COL_UUID, generateUUID());
        }
    }


    /**
     * 获取当前用户拥有的角色
     *
     * @return
     */
    public SystemRole getRole() {
        String sql = "select roleuuid from sys_role_user where useruuid =? and liveflag='1'";
        String rid = Db.queryColumn(sql, getStr(COL_UUID));
        return SystemRole.dao.findById(rid);
    }


    public List<SystemAttachmentFile> getAttachmentFiles() {
        String sql = "select s2.* from sys_user_m_files as s1 " +
                "left join sys_attachment_files as s2 on s1.attachmentuuid=s2.uuid " +
                "where s1.useruuid=?";
        return SystemAttachmentFile.dao.find(sql, this.get(COL_UUID));
    }


    public void addAttachmentFiles(String useruuid, String fileuuid) throws Exception {
        int i = Db.update(
                "insert into sys_user_m_files(uuid,useruuid,attachmentuuid,liveflag,createtime,remark) values(?,?,?,?,?,?)",
                UUIDKit.getUuidByJdk(true),useruuid, fileuuid, 1, DateTimeKit.getDate(), "");
        if (i == 1) {
            log.info("文件:" + fileuuid + "添加关联成功.");
        }
        else {
            throw new Exception("上传失败");
        }
    }
}
